package org.domain.pseample.session;

import java.util.HashMap;
import org.apache.log4j.Logger;

public class MapInstanceManager {

	private static Logger logger = Logger.getLogger(MapInstanceManager.class);

	private static MapInstanceManager mapInstanceManager;

	public synchronized static MapInstanceManager instance() {
		logger.info("in mapinstancemanager");
		if (mapInstanceManager == null) {
			mapInstanceManager = new MapInstanceManager();
		}
		return mapInstanceManager;
	}

	private HashMap<String, MapInstance> mapInstances = new HashMap<String, MapInstance>();

	synchronized public void setMapInstance(String userName) {
		if (mapInstances.get(userName) == null) {
			MapInstance mp = new MapInstance();
			mp.setUserName(userName);
			mapInstances.put(userName, mp);
			logger.info("successfully added " + userName + " to hashmap");
		}

	}

	synchronized public MapInstance getMapInstance(String userName) {
		/*
		 * if (mapInstances.get(userName) == null) { MapInstance mp = new
		 * MapInstance(); mp.setUserName(userName); mapInstances.put(userName,
		 * mp); }
		 */
		logger.info("successfully retreived " + userName + " from hashmap");
		return mapInstances.get(userName);

	}

	synchronized public void removeMapInstance(String userName) {
		mapInstances.remove(userName);
		logger.info("removed " + userName + " from haspmap");
	}

}
